<?php
/**
 * Models Base Class
 * User: zng
 * Date: 2019/9/11
 * Time: 09:11
 */
namespace app\admin\model\admin;

use think\model\concern\SoftDelete;
use app\admin\model\Models;
use app\admin\traits\model\Admin as AdminTiaits;

class Admin extends Models
{
    use SoftDelete,AdminTiaits;

    /**
     * 定义软删除标记字段
     * @var string
     */
    protected $deleteTime = 'delete_at';

    /**
     * 定义时间戳字段名
     * @var string
     */
    protected $createTime = 'create_at';

    /**
     * 定义时间戳字段名
     * @var string
     */
    protected $updateTime = 'update_at';

    /**
     * 设置时间戳格式
     * @var string
     */
    protected $autoWriteTimestamp = 'datetime';

    /**
     * 设置只读字段
     * @var array
     */
    protected $readonly = ['username'];

    /**
     * 登录查询
     * @param \think\Model $query
     * @return void
     */
    public function scopeLogin( $query, string $username )
    {
        $query->field('id,username,password,mobile,type,status,login_count,create_at,skin')->where('username', $username);
    }

    /**
     * 设置类型获取器
     * @return string
     */
    protected function getTypeTextAttr( $value, $data )
    {
        $type = config('field.admin.type');
        return $type[$data['type']]??'暂无';
    }

    /**
     * 设置状态获取器
     * @return string
     */
    protected function getStatusTextAttr( $value, $data )
    {
        $status = config('field.admin.status');
        return $status[$data['status']]??'暂无';
    }

    /**
     * 设置自动完成存储的值
     * @return string
     */
    protected function setPasswordAttr( $value )
    {
        return password_hash($value, PASSWORD_BCRYPT);
    }

}